variables:
  API_VERSION: v1
  MONGODB_IMAGE_TAG: mongo:6.0.5
  MONGODB_INTERNAL_PORT: 27017
  APP_IMAGE_TAG: $CI_REGISTRY_IMAGE/backend:$API_VERSION-$CI_COMMIT_SHORT_SHA
  APP_INTERNAL_PORT: 8080
  APP_STORE_API_KEY_ID: 85SUSZ47GY
  DOCKER_CONFIG_HOME: infrastructure/remote
  STAGE_ENVIRONMENT: stage
  STAGE_LOG_LEVEL: verbose
  STAGE_MONGODB_HOST: stage_mongodb
  STAGE_MONGODB_DB_NAME: main
  STAGE_USER_EMAIL_SENDING_ENABLED: "true"
  STAGE_YOOKASSA_MERCHANT_ID: 925997
  STAGE_SERVER_HOST: stage.angrywords.ru
  STAGE_SERVER_USER: cicd
  PROD_ENVIRONMENT: prod
  PROD_LOG_LEVEL: info
  PROD_MONGODB_HOST: mongodb
  PROD_MONGODB_DB_NAME: main
  PROD_USER_EMAIL_SENDING_ENABLED: "true"
  PROD_YOOKASSA_MERCHANT_ID: 320211
  PROD_SERVER_HOST: angrywords.ru
  PROD_SERVER_USER: cicd

services:
  - docker:20.10.18-dind

stages:
  - build
  - deploy_stage
  - deploy_prod

build:
  stage: build
  image: tiangolo/docker-with-compose:2023-05-01
  before_script:
    - apk update && apk add curl bash
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY_IMAGE
  script:
    - curl -s https://gitlab.com/gitlab-org/incubation-engineering/mobile-devops/download-secure-files/-/raw/main/installer | bash
    - ls -lah .secure_files
    - mkdir -p code/config
    - cp .secure_files/google_service_account.json code/config/
    - cp .secure_files/app_store_api_key.p8 code/config/
    - docker build -t $APP_IMAGE_TAG -f $DOCKER_CONFIG_HOME/Dockerfile .
    - docker push $APP_IMAGE_TAG
  only:
    - main

deploy_stage:
  stage: deploy_stage
  image: tiangolo/docker-with-compose:2023-05-01
  before_script:
    - apk update && apk add openssh-client
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY_IMAGE
    - echo "APP_IMAGE_TAG=$APP_IMAGE_TAG" >> $DOCKER_CONFIG_HOME/.env
    - echo "APP_INTERNAL_PORT=$APP_INTERNAL_PORT" >> $DOCKER_CONFIG_HOME/.env
    - echo "ENVIRONMENT=$STAGE_ENVIRONMENT" >> $DOCKER_CONFIG_HOME/.env
    - echo "LOG_LEVEL=$STAGE_LOG_LEVEL" >> $DOCKER_CONFIG_HOME/.env
    - echo "API_VERSION=$API_VERSION" >> $DOCKER_CONFIG_HOME/.env
    - echo "MONGODB_IMAGE_TAG=$MONGODB_IMAGE_TAG" >> $DOCKER_CONFIG_HOME/.env
    - echo "MONGODB_HOST=$STAGE_MONGODB_HOST" >> $DOCKER_CONFIG_HOME/.env
    - echo "MONGODB_INTERNAL_PORT=$MONGODB_INTERNAL_PORT" >> $DOCKER_CONFIG_HOME/.env
    - echo "MONGODB_DB_NAME=$STAGE_MONGODB_DB_NAME" >> $DOCKER_CONFIG_HOME/.env
    - echo "API_ACCESS_TOKEN_SECRET_KEY=$API_ACCESS_TOKEN_SECRET_KEY" >> $DOCKER_CONFIG_HOME/.env
    - echo "NOREPLY_EMAIL_BOX_PASSWORD=$NOREPLY_EMAIL_BOX_PASSWORD" >> $DOCKER_CONFIG_HOME/.env
    - echo "USER_EMAIL_SENDING_ENABLED=$STAGE_USER_EMAIL_SENDING_ENABLED" >> $DOCKER_CONFIG_HOME/.env
    - echo "YOOKASSA_MERCHANT_ID=$STAGE_YOOKASSA_MERCHANT_ID" >> $DOCKER_CONFIG_HOME/.env
    - echo "YOOKASSA_SECRET_KEY=$STAGE_YOOKASSA_SECRET_KEY" >> $DOCKER_CONFIG_HOME/.env
    - echo "APP_STORE_API_KEY_ID=$APP_STORE_API_KEY_ID" >> $DOCKER_CONFIG_HOME/.env
    - echo "APP_STORE_SHARED_SECRET=$APP_STORE_SHARED_SECRET" >> $DOCKER_CONFIG_HOME/.env
    - echo "AMPLITUDE_API_KEY=$STAGE_AMPLITUDE_API_KEY" >> $DOCKER_CONFIG_HOME/.env
  script:
    - mkdir ~/.ssh/
    - ssh-keyscan $STAGE_SERVER_HOST >> ~/.ssh/known_hosts
    - cp $STAGE_SSH_KEY ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
    - DOCKER_HOST=ssh://$STAGE_SERVER_USER@$STAGE_SERVER_HOST
    - docker compose -f "$DOCKER_CONFIG_HOME/docker-compose-stage.yml" -p "stage_backend" down --remove-orphans
    - docker compose -f "$DOCKER_CONFIG_HOME/docker-compose-stage.yml" -p "stage_backend" pull
    - docker compose -f "$DOCKER_CONFIG_HOME/docker-compose-stage.yml" -p "stage_backend" up -d
  only:
    - main

deploy_prod:
  stage: deploy_prod
  image: tiangolo/docker-with-compose:2023-05-01
  before_script:
    - apk update && apk add openssh-client
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY_IMAGE
    - echo "APP_IMAGE_TAG=$APP_IMAGE_TAG" >> $DOCKER_CONFIG_HOME/.env
    - echo "APP_INTERNAL_PORT=$APP_INTERNAL_PORT" >> $DOCKER_CONFIG_HOME/.env
    - echo "CI_REGISTRY_IMAGE=$CI_REGISTRY_IMAGE" >> $DOCKER_CONFIG_HOME/.env
    - echo "ENVIRONMENT=$PROD_ENVIRONMENT" >> $DOCKER_CONFIG_HOME/.env
    - echo "LOG_LEVEL=$PROD_LOG_LEVEL" >> $DOCKER_CONFIG_HOME/.env
    - echo "API_VERSION=$API_VERSION" >> $DOCKER_CONFIG_HOME/.env
    - echo "MONGODB_IMAGE_TAG=$MONGODB_IMAGE_TAG" >> $DOCKER_CONFIG_HOME/.env
    - echo "MONGODB_HOST=$PROD_MONGODB_HOST" >> $DOCKER_CONFIG_HOME/.env
    - echo "MONGODB_INTERNAL_PORT=$MONGODB_INTERNAL_PORT" >> $DOCKER_CONFIG_HOME/.env
    - echo "MONGODB_DB_NAME=$PROD_MONGODB_DB_NAME" >> $DOCKER_CONFIG_HOME/.env
    - echo "API_ACCESS_TOKEN_SECRET_KEY=$API_ACCESS_TOKEN_SECRET_KEY" >> $DOCKER_CONFIG_HOME/.env
    - echo "NOREPLY_EMAIL_BOX_PASSWORD=$NOREPLY_EMAIL_BOX_PASSWORD" >> $DOCKER_CONFIG_HOME/.env
    - echo "USER_EMAIL_SENDING_ENABLED=$PROD_USER_EMAIL_SENDING_ENABLED" >> $DOCKER_CONFIG_HOME/.env
    - echo "YOOKASSA_MERCHANT_ID=$PROD_YOOKASSA_MERCHANT_ID" >> $DOCKER_CONFIG_HOME/.env
    - echo "YOOKASSA_SECRET_KEY=$PROD_YOOKASSA_SECRET_KEY" >> $DOCKER_CONFIG_HOME/.env
    - echo "APP_STORE_API_KEY_ID=$APP_STORE_API_KEY_ID" >> $DOCKER_CONFIG_HOME/.env
    - echo "APP_STORE_SHARED_SECRET=$APP_STORE_SHARED_SECRET" >> $DOCKER_CONFIG_HOME/.env
    - echo "AMPLITUDE_API_KEY=$PROD_AMPLITUDE_API_KEY" >> $DOCKER_CONFIG_HOME/.env
  script:
    - mkdir ~/.ssh/
    - ssh-keyscan $PROD_SERVER_HOST >> ~/.ssh/known_hosts
    - cp $PROD_SSH_KEY ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
    - DOCKER_HOST=ssh://$PROD_SERVER_USER@$PROD_SERVER_HOST
    - docker compose -f "$DOCKER_CONFIG_HOME/docker-compose-prod.yml" -p "backend" down --remove-orphans
    - docker compose -f "$DOCKER_CONFIG_HOME/docker-compose-prod.yml" -p "backend" pull
    - docker compose -f "$DOCKER_CONFIG_HOME/docker-compose-prod.yml" -p "backend" up -d
  only:
    - main
  when: manual
  allow_failure: false
